<?php
defined('IN_IA') or exit('Access Denied');
global $_W, $_GPC;
$setting = $this->baseset($_W['uniacid']);
load()->func('communication');
$page = $_GPC['page1'];
$pageend = $_GPC['pageend1'];
if (empty($_GPC['no'])) {
	$_GPC['no'] = $page;
	$thispage = $page;
} else {
	$thispage = $_GPC['no'];
}
$newpage = $_GPC['no'] + 1;
$dtkurl = "http://api.dataoke.com/index.php?r=Port/index&type=total&appkey=" . $setting['dtk_key'] . "&v=2&page=" . $thispage;
$resp = ihttp_get($dtkurl);
if (is_error($resp)) {
	$this->json_exit(0, $resp["message"]);
}
$dtkData = json_decode($resp['content'], true);
if ($dtkData['data']['total_num'] >= 1) {
	$result = $dtkData['result'];
	$total = $dtkData['data']['total_num'];
	$total_i = 0;
	foreach ($result as $key => $item) {
		if ($total_i < $total) {
			if ($item['GoodsID'] == '') {
				break;
			}
			if ($item['GoodsID'] != '') {
				$cate = pdo_fetch('select id,itemid from ' . tablename('bsht_tbk_all_caiji') . " where uniacid = '{$uniacid}' AND itemid = '{$item['GoodsID']}' and uniacid = '{$_W['uniacid']}' limit 1");
				$newurl = "https://uland.taobao.com/coupon/edetail?activityId=" . $item['Quan_id'] . "&pid=" . $setting['tb_pid'] . "&itemId=" . $item['GoodsID'] . "&src=pgy_pgyqf&dx=1";
				$logo = $item['Pic'];
				$text = $item['D_title'] . "【券后价￥" . $item['Price'] . "】";
				if ($item['Commission_queqiao'] > $item['Commission_jihua']) {
					$bili = $item['Commission_queqiao'];
				} else {
					$bili = $item['Commission_jihua'];
				}
				$data = array('atime' => TIMESTAMP, 'uniacid' => $_W['uniacid'], 'displayorder' => '0', 'itemid' => $item['GoodsID'], 'title' => $item['Title'], 'd_title' => $item['D_title'], 'itempic' => $item['Pic'], 'cate_id' => $item['Cid'], 'shoptitle' => $item['Introduce'], 'itemfee' => $item['Org_Price'], 'itemfee2' => $item['Price'], 'itemmsell' => $item['Sales_num'], 'istmall' => $item['IsTmall'], 'itemyhj_fee' => $item['Quan_price'], 'itemyhj_zl' => $item['Quan_surplus'], 'itemyhj_yl' => $item['Quan_surplus'] - $item['Quan_receive'], 'itemyhj_re' => $item['Quan_receive'], 'link_jihua' => $item['Jihua_link'], 'com_jihua' => $item['Commission_jihua'], 'com_queqiao' => $item['Commission_queqiao'], 'status' => 1, 'wenan' => $item['Introduce'], 'itemyhj_tit' => $item['Quan_condition'], 'itemyhj_stime' => date("Y-m-d H:i:s", time()), 'itemyhj_etime' => $item['Quan_time'], 'itemyhj_url' => $newurl, 'itemurl' => $newurl, 'quan_link' => $item['Quan_link'], 'shouru_bili' => $bili, 'shouru_yongjin' => $item['Org_Price'] * $bili / 100, 'quan_id' => $item['Quan_id'], 'maijia_id' => $item['SellerID'], 'fm' => 'dtk',);
				$data['itemsurl'] = $newurl;
				$data['itemyhj_tkl'] = '';
				$data['itemtkl'] = '';
				if (empty($cate)) {
					pdo_insert('bsht_tbk_all_caiji', $data);
					$cateid = pdo_insertid();
				} else {
					pdo_update($this->modulename . '_all_caiji', $data, array('id' => $cate['id']));
					$cateid = $cate['id'];
				}
			}
			$total_i++;
		} else {
		}
	}
	if ($thispage == $pageend) {
		message('采集成功！', $this->createWebUrl('listcaijiall_dtk'), 'success');
	} else {
		message('即将导入第' . $newpage . '页（每页200条），请稍后。。。', $this->createWebUrl('importall_dtk1', array('page1' => $page, 'pageend1' => $pageend, 'no' => $newpage)), 'success');
	}
} else {
}
message('采集成功！', $this->createWebUrl('listcaijiall_dtk'), 'success');